package Day12;

import Day07.Array;

import javax.lang.model.type.ArrayType;
import java.util.ArrayList;
import java.util.Scanner;

public class Students {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        loop: while(true){
            System.out.println("---------欢迎来到学生管理系统---------");
            System.out.println("1：添加学生");
            System.out.println("2:  删除学生");
            System.out.println("3:  修改学生");
            System.out.println("4： 查询学生");
            System.out.println("5:  退出");
            System.out.println("请输入你的选择");
            String choose = sc.next();
            switch(choose){
                case "1":
                    addStudent(list);
                    continue;
                case "2":
                    deleteStudent(list);
                    continue;
                case "3":
                    updateStudent(list);
                    continue;
                case "4":
                    seleteStudent(list);
                    continue;
                case "5":
                    System.out.println("退出");
                    break loop;
                default:
                    System.out.println("输入的选项有误,请重新输入");
                    continue;
            }

        }
    }


    public static void addStudent(ArrayList<Student> list){
        Student s = new Student();

        System.out.println("请输入要添加的学生的ID");
        Scanner sc = new Scanner(System.in);
        String id =null;
        while(true){
            //如果重复，要不停地输入
            id = sc.next();
            boolean flag  = unique(list,id);
            if(flag){
                System.out.println("ID已经重复了，请重新输入");
            }else{
                s.setId(id);
                break;
            }
        }
        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄");
        String age = sc.next();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        s.setAddress(address);

        list.add(s);

    }
    public static void deleteStudent(ArrayList<Student> list){
       Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的学生的ID");
        String sid = sc.next();
        int index = selectindex(list,sid);
        if(index >=0){
            //索引存在，删除
            list.remove(index);
            System.out.println("学生信息删除成功");
        }else{
            System.out.println("ID不存在");
            return;
        }


    }
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的ID");
        String id = null;

            id = sc.next();
            int index = selectindex(list,id);
            if(index < 0){
                System.out.println("输入的ID不存在");
                return;
            }else{
                Student stu = list.get(index);
                stu.setId(id);
                System.out.println("请输入新的学生的姓名");
                String name = sc.next();
                stu.setName(name);

                System.out.println("请输入学生的新的年龄");
                String age = sc.next();
                stu.setAge(age);

                System.out.println("请输入学生的新的家庭住址");
                String address = sc.next();
                stu.setAddress(address);
            }


        }
    public static void seleteStudent(ArrayList<Student> list){
        Student stu = new Student();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            System.out.println(stu.getId() +"   "+ stu.getName()+
                    " "+stu.getAge() + "  "+ stu.getAddress());
        }

    }
    public static boolean  unique(ArrayList<Student> list ,String id){
        Student stu = new Student();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                //已经重复，返回true
                return true;
            }
        }
        //没有重复的ID，返回false
        return false;

    }

    public static int selectindex(ArrayList<Student> list,String id){
        Student stu = new Student();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                //ID存在，返回索引
                return i;
            }
        }
        return -1;
    }


}
